; fire20.asm
; Fire effect (Vic-20)

	.macpack cbm

	VIDEO_BASE	= $1C00
	CHAR_BASE	= $1000
	COLOR_BASE  = $9400
	
	CHAR_GEN = $8000
	
	TEXT_BUF1 = $1200
	TEXT_BUF2 = $1500
	
	TEXT_ROW = 8
	
	VIDEO_BUFFER = $1800
	
	VIDEO_PAGES = 4

	VIC_BASE = $9000
	VIA1_BASE = $9110
	VIA2_BASE = $9120

	IRQ_VECTOR = $0314
	NMI_VECTOR = $0318
	IRQ_TAIL = $EB15

	PAL_COLS = 28
	PAL_ROWS = 35

	PAL_TIMER_VALUE = 	312 * 71 - 2		; Original: $4826
	PAL_LINE = $60		; raster line (Original: 288/289)

	NMI_TIMER_VALUE = $B1

	FIRST_CHAR1 = $40
	FIRST_CHAR2 = $A0

	COLOR_BLACK = 0
	COLOR_WHITE = 1
	COLOR_RED = 2
	COLOR_CYAN = 3
	COLOR_PURPLE = 4
	COLOR_GREEN = 5
	COLOR_BLUE = 6
	COLOR_YELLOW = 7
	
	COLOR_ORANGE = 8
	COLOR_LIGHT_ORANGE = 9
	COLOR_LIGHT_RED = 10
	COLOR_LIGHT_CYAN = 11
	COLOR_LIGHT_PURPLE = 12
	COLOR_LIGHT_GREEN = 13
	COLOR_LIGHT_BLUE = 14
	COLOR_LIGHT_YELLOW = 15
	
	.segment "ZEROPAGE"
	
ptr:		.res 2
cnt:		.res 1
hlpA:		.res 1
hlpD:		.res 1		 
vsync_flg:	.res 1
txpix:		.res 1
txpos:		.res 1
delay:		.res 1
sampptr:	.res 2

	.segment "CODE"

init:		; clear screen & set-up char RAM
			ldx #0
			txa
			sta txpos
			sta txpix
@clrvideo:	sta VIDEO_BASE+$0000, x	
			sta VIDEO_BASE+$0100, x	
			sta VIDEO_BASE+$0200, x	
			sta VIDEO_BASE+$0300, x
			inx
			bne @clrvideo
			txa
@clrchars:	sta CHAR_BASE+$0200, x	
			sta CHAR_BASE+$0300, x	
			sta CHAR_BASE+$0400, x	
			sta CHAR_BASE+$0500, x	
			sta CHAR_BASE+$0600, x	
			sta CHAR_BASE+$0700, x	
			sta CHAR_BASE+$0800, x	
			sta CHAR_BASE+$0900, x	
			sta CHAR_BASE+$0A00, x	
			sta CHAR_BASE+$0B00, x	
			inx
			bne @clrchars
@cploop:	lda table2+$0000, x
			sta CHAR_BASE+$0000, x
			lda table2+$0100, x
			sta CHAR_BASE+$0100, x
			inx
			bne @cploop
			; set-up colors
			lda #COLOR_ORANGE*16
			sta VIC_BASE+14
			lda #COLOR_RED+8
			sta VIC_BASE+15
			lda #COLOR_YELLOW+8
@clrcol:	sta COLOR_BASE+$0000, x	
			sta COLOR_BASE+$0100, x	
			sta COLOR_BASE+$0200, x	
			sta COLOR_BASE+$0300, x
			inx
			bne @clrcol
			; setup video mode (PAL)
			lda	#6				; horizontal line start
			sta	VIC_BASE+0
			lda #160			; first line
			sta VIC_BASE+1
			lda #PAL_COLS		; columns
			sta VIC_BASE+2
			lda VIC_BASE+3
			and #$80
			ora #PAL_ROWS*2		; rows & char height (single)
			sta VIC_BASE+3
			lda #$FC			; video & character address 
			sta VIC_BASE+5
			
			; set-up scroll-text buffer
			ldx #$F0-1
			lda #0
@clrtxt:	sta scrlbuf+0*$F0, x
			sta scrlbuf+1*$F0, x
			sta scrlbuf+2*$F0, x
			dex
			bpl @clrtxt
			
			; init sound
			lda	#0
			sta VIC_BASE+10
			sta VIC_BASE+11
			sta VIC_BASE+12
			sta VIC_BASE+13

			lda #<sample
			sta sampptr
			lda #>sample
			sta sampptr+1
			lda #1
			sta delay
			
			; setup sound interrupt

			lda #$7F				; disable all interrupts
			sta VIA1_BASE+14

			lda #<sound_nmi
			ldx #>sound_nmi
			sta NMI_VECTOR
			stx NMI_VECTOR+1
			
			lda #<NMI_TIMER_VALUE
			sta VIA1_BASE+4
			lda #>NMI_TIMER_VALUE
			sta VIA1_BASE+5
			
			lda #$7F
			sta VIA1_BASE+14
			lda #$C0
			sta VIA1_BASE+14

			; done - init IRQ
			lda #0
			sta vsync_flg
			lda #<PAL_TIMER_VALUE
			sta VIA2_BASE+4
			ldx #>PAL_TIMER_VALUE
			lda #PAL_LINE
			bne irqsync
irqsync:	cmp VIC_BASE+4
			bne irqsync
			stx VIA2_BASE+5		; load T1 latch high, and transfer both bytes to T1 counter

			; setup interrupt handler
			lda #<timer_irq
			ldx #>timer_irq
			sei
			sta IRQ_VECTOR
			stx IRQ_VECTOR+1
			cli
			
			lda #$C0			; Enable timer NMI
			sta VIA1_BASE+14
			
			;
			; main loop
			;

mainloop:	; calc fire
			inc cnt
			ldx #PAL_COLS+1
@loop6:		lda CHAR_BASE, x
			adc cnt
			tay
			lda CHAR_BASE, y
			lsr
			lsr
			sta VIDEO_BUFFER+PAL_COLS*PAL_ROWS-1, x		; invisible line 26
			dex
			bne @loop6
			stx ptr
			lda #1+>VIDEO_BUFFER
			sta ptr+1
			lda #VIDEO_PAGES-1
			sta hlpD
@loop7:		clc
			ldy #PAL_COLS-1
			lda (ptr), y
			iny
			adc (ptr), y
			iny
			adc (ptr), y
			ldy #0
			adc (ptr), y
			sbc #3
			bcs @nxt7
			lda #0
@nxt7:		lsr
			lsr
			sta (ptr), y
			inc ptr
			bne @nxt8
			inc ptr+1
@nxt8:		dex
			bne @loop7
			
			dec hlpD
			bne @loop7
			
scroll:		; do scroller
			lda txpix
			and #7
			bne @ctch
			; put next char
			ldx txpos
			lda text, x
			bpl @intxt
			ldx #0
			sta txpos
			lda text, x
@intxt:		inc txpos
			tax
			bne @setfp
			ldx #32 
@setfp:		dex
			lda fntmap_lo, x
			sta @fntptr
			lda fntmap_hi, x
			sta @fntptr+1
			lda #<(scrlbuf+PAL_COLS)
			sta ptr
			lda #>(scrlbuf+PAL_COLS)
			sta ptr+1
			ldy #0
			ldx #0
			
@ldfnt:		lda font, x
	@fntptr = @ldfnt+1
			sta (ptr), y
			clc
			lda ptr
			adc #PAL_COLS+2
			sta ptr
			bcc @nohip
			inc ptr+1
@nohip:		inx
			cpx #24
			bcc @ldfnt
			tya
			bne @ctch
			tax
			lda #<(scrlbuf+PAL_COLS)
			sta ptr
			lda #>(scrlbuf+PAL_COLS)
			sta ptr+1
			iny
			clc
			lda @fntptr
			adc #24
			sta @fntptr
			bcc @nohif
			inc @fntptr+1
@nohif:		jmp @ldfnt

@ctch:		; scroll left (2x)
			ldy #2
@left:		ldx #$F0
@scrl1:		rol scrlbuf+2*$F0-1, x
			dex
			bne @scrl1
			ldx #$F0
@scrl2:		rol scrlbuf+1*$F0-1, x
			dex
			bne @scrl2
			ldx #$F0
@scrl3:		rol scrlbuf+0*$F0-1, x
			dex
			bne @scrl3
			dey
			bne @left
			inc txpix
			
			lda txpix
			and #1
			tax
			; copy to buf (i)
			lda bufp_lo, x
			sta ptr
			lda bufp_hi, x
			sta ptr+1
			ldx #0
@bclp:		ldy #0
			.repeat	3*8, I
			lda scrlbuf+I*(PAL_COLS+2), x
			sta (ptr), y
			iny
			.endrep
			clc
			tya
			adc ptr
			sta ptr
			bcc @nohir
			inc ptr+1			
@nohir:		inx
			cpx #PAL_COLS
			beq @donebc
			jmp @bclp
@donebc:			

show:		; init video buf (i)
			lda #PAL_COLS
			sta hlpA
			lda txpix
			and #1
			tax
			lda char_idx, x
			tax
			ldy #0
@vloop:		txa
			sta VIDEO_BUFFER+TEXT_ROW*PAL_COLS, y
			inx
			txa
			sta VIDEO_BUFFER+(TEXT_ROW+1)*PAL_COLS, y
			inx
			txa
			sta VIDEO_BUFFER+(TEXT_ROW+2)*PAL_COLS, y
			inx
			iny
			dec hlpA
			bne @vloop

			; wait retrace
			lda #1
			sta vsync_flg
@pollwait:	cmp vsync_flg
			beq @pollwait

;			lda #COLOR_RED+8+16*COLOR_BLUE
;			sta VIC_BASE+15

			; copy buffer
			ldx #0
@bufloop:	lda VIDEO_BUFFER + $0000, x 
			sta VIDEO_BASE + $0000, x
			lda VIDEO_BUFFER + $0100, x 
			sta VIDEO_BASE + $0100, x
			lda VIDEO_BUFFER + $0200, x 
			sta VIDEO_BASE + $0200, x
			lda VIDEO_BUFFER + $0300, x 
			sta VIDEO_BASE + $0300, x
			inx
			bne @bufloop

;			lda #COLOR_RED+8
;			sta VIC_BASE+15

			jmp mainloop

;----------------------------------------------------------------------------
timer_irq:	lda vsync_flg
			beq @done_sync
			dec vsync_flg
@done_sync:
			lda VIC_BASE+1
			cmp #16
			bcc @done_scrl
			dec VIC_BASE+1

@done_scrl:
			jmp IRQ_TAIL

;----------------------------------------------------------------------------
sound_nmi:	dec delay
			bne @done
			
			sta @storea+1
			sty @storey+1
			
			ldy #0
			lda (sampptr), y
			bne @insample
			
			lda #<sample
			sta sampptr
			lda #>sample
			sta sampptr+1

			lda (sampptr), y
			
@insample:	sta delay
			iny
			lda (sampptr), y
			ora #COLOR_ORANGE*16
			sta VIC_BASE+14			
			
			clc
			lda sampptr
			adc #2
			sta sampptr
			bcc @nohi
			inc sampptr+1
@nohi:
		
@storea:	lda #0
@storey:	ldy #0 

@done:		bit VIA1_BASE+4
			rti
			
	.segment "DATA"

scrlbuf:	.repeat 3*8	
				.res PAL_COLS+2
			.endrep

	.segment "RODATA"
	
text:		.byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
			scrcode "greetings to the denial community from kananga ...   "
			scrcode "original fire effect : flugeldufel/c-64   "
			scrcode "fontwar : jeffrey daniels   "
			scrcode "noise : amix & kananga"
			.byte $FF
	
fntmap_lo:	.repeat 63, I
				.byte <(font + 48*I)
			.endrep

fntmap_hi:	.repeat 63, I
				.byte >(font + 48*I)  
			.endrep

bufp_lo:	.byte <TEXT_BUF1, <TEXT_BUF2
bufp_hi:	.byte >TEXT_BUF1, >TEXT_BUF2
char_idx:	.byte FIRST_CHAR1, FIRST_CHAR2
				
table1:		.incbin "firetab.bin"
	
table2:		.incbin "firechars.bin"
	
font:		.incbin "scrlfnt.bin"

sample:		.incbin "sample.bin"
